﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Runtime.Intrinsics.Arm;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Domain.Repositories;

namespace Himp.TaskScheduling
{
    /// <summary>
    /// 标准化验任务
    /// </summary>
    public class StandardTestTaskService : TaskSchedulingAppService, IStandardTestTaskService
    {
        private IRepository<StandardTestTask, long> _standardTestTaskRepository;
        private IRepository<StandardTestItem, long> _standardTestItemRepository;
        private IRepository<SampleTestTask, long> _sampleTestTaskRepository;

        public StandardTestTaskService(IRepository<StandardTestTask, long> standardTestTaskRepository
            , IRepository<StandardTestItem, long> standardTestItemRepository
            , IRepository<SampleTestTask, long> sampleTestTaskRepository)
        {
            _standardTestTaskRepository = standardTestTaskRepository;
            _standardTestItemRepository = standardTestItemRepository;
            _sampleTestTaskRepository = sampleTestTaskRepository;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<PagedResultDto<StandardTestTaskDto>> GetListAsync(StandardTestTaskInput input)
        {
            var iQuery = await _standardTestTaskRepository.GetQueryableAsync();
            iQuery = iQuery.WhereIf(input.Begntime != null, i => i.TaskAssgnTime >= input.Begntime);
            iQuery = iQuery.WhereIf(input.Endtime != null, i => i.TaskAssgnTime <= input.Endtime.Value.AddDays(1));
            iQuery = iQuery.OrderByDescending(i => i.LastOperTime);

            if (input.Pi == 0)
            {
                var itemsDto = ObjectMapper.Map<List<StandardTestTask>, List<StandardTestTaskDto>>(iQuery.ToList());

                return new PagedResultDto<StandardTestTaskDto>(itemsDto.Count, itemsDto);
            }
            else
            {
                var totalCount = iQuery.Count();
                var items = iQuery.Page(input.Pi, input.Ps);
                var itemsDto = ObjectMapper.Map<List<StandardTestTask>, List<StandardTestTaskDto>>(items.ToList());

                return new PagedResultDto<StandardTestTaskDto>(totalCount, itemsDto);
            }
        }

        /// <summary>
        /// 审核
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task PostAuditAsync(StandardTestTaskAuditDto input)
        {
            var standardTestTaskList = await _standardTestTaskRepository.GetListAsync(
                i => input.StandardTestTaskId != null && input.StandardTestTaskId.Contains(i.Id)
                && i.ExecStas =="未就绪" );

            var standardTestItems = await _standardTestItemRepository.GetListAsync(i => i.ValiFlag);

            foreach (var item in standardTestTaskList)
            {
                var testItems = "";
                var standardTest = standardTestItems.FirstOrDefault(s => s.SampleType == item.Sampletype);

                if (standardTest != null)
                {
                    testItems = standardTest.TestItems;
                }

                SampleTestTask sampleTestTask = new SampleTestTask(item.SampleCode
                    , item.TestCode
                    , item.BottomCode
                    , item.Sampletype
                    , 2
                    , testItems
                    , 0);

                item.ExecStas = "已审核";
                sampleTestTask = await _sampleTestTaskRepository.InsertAsync(sampleTestTask);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="samplePreparationTaskId"></param>
        /// <returns></returns>
        public async Task<List<StandardTestItemDto>> GetItemListAsync()
        {
            var iQuery = await _standardTestItemRepository.GetQueryableAsync();
            var itemsDto = ObjectMapper.Map<List<StandardTestItem>, List<StandardTestItemDto>>(iQuery.ToList());

            return itemsDto;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <param name="valiFlag"></param>
        /// <returns></returns>
        public async Task PutItemValiAsync(long id, bool valiFlag)
        {
            var item = await _standardTestItemRepository.GetAsync(id);
            item.ValiFlag = valiFlag;
        }
    }
}
